package com.paschino.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.paschino.modal.Album;

@Repository
public class AlbumDAOImpl extends AbstractDAO implements AlbumDAO {

	@Override
	public boolean addAlbum(Album album) {
		getSession().saveOrUpdate(album);
		if (album.getId() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateAlbum(Album album) {
		getSession().update(album);
		return true;
	}

	@Override
	public Album getAlbum(Integer id) {
		return (Album) getSession().get(Album.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getAlbumList() {
		List<Album> albumList = new ArrayList<Album>();
		Query query = getSession().createQuery("from Album a where (a.isDelete is null or a.isDelete = false)");
		albumList = query.list();
		return albumList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getAlbumListByActive() {
		List<Album> albumList = new ArrayList<Album>();
		Query query = getSession().createQuery("from Album a where (a.isDelete is null or a.isDelete = false) and a.isActive = true ");
		albumList = query.list();
		return albumList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Album getAlbumByName(String name){
		List<Album> albumList = new ArrayList<Album>();
		Query query = getSession().createQuery("from Album a where (a.isDelete is null or a.isDelete = false) and a.name = ?");
		query.setParameter(0, name);
		albumList = query.list();
		Album album = new Album();
		if(albumList.size() == 1){
			for(int i = 0; i < albumList.size(); i++){
				album = albumList.get(i);
			}
		}
		return album;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getLibraryAlbum(){
		List<Album> albumList = new ArrayList<Album>();
		Query query = getSession().createQuery("from Album a where (a.isDelete is null or a.isDelete = false) and a.name not in ('Slider', 'Career') order by a.id desc limit 10");
		albumList = query.list();
		return albumList;
	}
	
	@Override
	public int deleteAlbum(int albumID){
		Query query = getSession().createQuery("update Album a set a.isDelete = true where a.id = :albumID");
		query.setParameter("albumID", albumID);
		int result = query.executeUpdate();
		return result;
	}

	@Override
	public int hideAlbum(int albumID){
		Query query = getSession().createQuery("update Album a set a.isActive = false where a.id = :albumID");
		query.setParameter("albumID", albumID);
		int result = query.executeUpdate();
		return result;
	}
	
	@Override
	public int unhideAlbum(int albumID){
		Query query = getSession().createQuery("update Album a set a.isActive = true where a.id = :albumID");
		query.setParameter("albumID", albumID);
		int result = query.executeUpdate();
		return result;
	}
}
